var app = getApp();
Page({
    data: {
        date: "", // 日期
        time: "", // 时间
        member: [], // 成员列表
        index: 0,

        id: 0, // 当前编辑id
        title: "", // 活动标题
        start_time: "", // 开始时间
        margin: "", // 保证金
        commission: "", // 最低佣金
        percent: "", // 佣金费率
        host_name: "", // 主持人姓名
        host_user_id: "", // 主持人id

        status: true, // 是否未提交过
    },
    // formid
    formSubmit: function(e) {
        console.log(e.detail.formId)
        var formId = e.detail.formId

        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/user/collect_formid',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        formid: formId
                    },
                    success: function(res) {
                        if (res.data.code == 1) {


                        }

                    }
                })
            }
        })
    },

    inputText(e) {
        var type = e.target.dataset.type;
        var value = e.detail.value;

        if (type == "title") {
            this.setData({
                title: value
            });
            return
        }
        if (type == "margin") {
            this.setData({
                margin: value
            });
            return
        }
        if (type == "commission") {
            this.setData({
                commission: value
            });
            return
        }
        if (type == "percent") {
            this.setData({
                percent: value
            });
            return
        }

    },
    // 选择日期 
    bindDateChange(e) {
        this.setData({
            date: e.detail.value
        })
    },
    // 选择时间
    bindTimeChange(e) {
        this.setData({
            time: e.detail.value
        })
    },
    // 选择主持人
    bindPickerChange(e) {
        var member = this.data.member;
        this.setData({
            host_name: member[e.detail.value].name,
            host_user_id: member[e.detail.value].user_id,
            index: e.detail.value
        })
    },
    // 保存前验证
    saveVerify() {
        var data = {};
        var date = this.data.date;
        var time = this.data.time;
        data.id = this.data.id;
        data.title = this.data.title;
        data.start_time = date + ' ' + time;
        data.margin = this.data.margin;
        data.commission = this.data.commission;
        data.percent = this.data.percent;
        data.host_name = this.data.host_name;
        data.host_user_id = this.data.host_user_id;

        if (!data.title) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入拍卖标题',
            })
            return false;
        }
        if (!date) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请选择开始日期',
            })
            return false;
        }
        if (!time) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请选择开始时间',
            })
            return false;
        }
        if (!data.margin) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入保证金',
            })
            return false;
        }
        if (!data.commission) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入最低佣金',
            })
            return false;
        }
        if (!data.percent) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入佣金费率',
            })
            return false;
        }
        if (!data.host_name) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请选择主持人',
            })
            return false;
        }
        if (!data.host_user_id) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '主持人id不能为空',
            })
            return false;
        }

        this.save(data);
    },
    // 保存编辑
    save(data) {
        var self = this;
        var status = this.data.status;
        var url = data.id ? '/auction/edit_auction' : '/auction/addauction';
        if (status) {
            self.setData({status: false})
            wx.getStorage({
                key: 'user',
                success: function(res) {
                    data.openid = res.data.openid;
                    data.token = res.data.token;
                    wx.request({
                        url: app.url.requestUrl + url,
                        method: 'POST',
                        data: data,
                        success: function(res) {
                            var datas = res.data;
                            if (datas.code == 1) {
                                wx.redirectTo({
                                    url: '../my-lots-list/my-lots-list?id=' + (data.id ? data.id : datas.data.id),
                                })
                            } else {
                                self.setData({ status: true })
                                wx.showToast({
                                    image: '../../img/warning.png',
                                    title: datas.msg,
                                })
                            }
                        }
                    })
                }
            })
        }

    },
    onLoad: function(options) {
        this.setData({
            id: options.id ? options.id : ''
        })
        var self = this;

        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/personal/my_team',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                    },
                    success: function(res) {
                        var data = res.data;
                        if (data.code == 1) {
                            self.setData({
                                member: data.data,
                                host_name: data.data[0].name,
                                host_user_id: data.data[0].user_id
                            })
                        } else {
                            wx.showToast({
                                image: '../../img/warning.png',
                                title: data.msg,
                            })
                        }
                    }
                })
            }
        })

        if (options.id) {
            wx.getStorage({
                key: 'user',
                success: function(res) {
                    wx.request({
                        url: app.url.requestUrl + '/auction/edit_auction_data',
                        method: 'POST',
                        data: {
                            openid: res.data.openid,
                            token: res.data.token,
                            id: options.id,
                        },
                        success: function(res) {
                            var data = res.data;
                            if (data.code == 1) {
                                var dataTime = data.data.start_time.split(" ");
                                var date = dataTime[0];
                                var time = dataTime[1];
                                var index = self.data.index;
                                self.data.member.map((item, i) => {
                                    if (item.user_id == data.data.host_user_id) {
                                        index = i;
                                    }
                                })
                                self.setData({
                                    title: data.data.title,
                                    date: date,
                                    time: time,
                                    commission:data.data.commission,
                                    margin: data.data.margin,
                                    start_price: data.data.start_price,
                                    percent: data.data.percent,
                                    host_user_id: data.data.host_user_id,
                                    host_name: data.data.host_name,
                                    index: index,
                                })
                            } else {
                                wx.showToast({
                                    image: '../../img/warning.png',
                                    title: data.msg,
                                })
                            }
                        }
                    })
                }
            })
        }
    }
})